﻿#region NameSpace
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Eb.StockManagement;
using Eb.MessageBox;
using ebird.BusinessProcessAutomationDAS;
#endregion

/// <summary>
/// Displayz the current product Status
/// </summary>
public partial class Modules_StockManagement_StockStatus : System.Web.UI.UserControl
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    string connectionString = ConfigurationManager.ConnectionStrings["connectUserManager"].ToString();
    Eb.StockManagement.StockManagement stockMgmt = new Eb.StockManagement.StockManagement();
    ebird.BusinessProcessAutomationDAS.StockManagementDAS stockManagementDAS;

    protected void Page_Load(object sender, EventArgs e)
     {
         stockManagementDAS = new ebird.BusinessProcessAutomationDAS.StockManagementDAS(connectionString);
         if (this.IsPostBack)
         {
             Label1.Visible = true;
             Label1.Text = "Searching..";
             System.Threading.Thread.Sleep(500);
              Label1.Visible = false;
              Label1.Text = "";
         }
        if (!IsPostBack)
            GetProductStatus();
    }

    protected void rblCriteria_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void ddlSelect_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void btnBack_Click(object sender, EventArgs e)
    {

    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
       // System.Threading.Thread.Sleep(3000);
        GetProductStatus();
    }

    protected void PopulateCondition(DropDownList ddlSelect)
    {
        ddlSelect.Items.Add("=");
        ddlSelect.Items.Add("<>");
        ddlSelect.Items.Add(">");
        ddlSelect.Items.Add("<");
        ddlSelect.Items.Insert(0, "All");
    }
    protected void PopulateProductCode(DropDownList ddlSelect)
    {
        ddlSelect.Items.Add("Like");
        ddlSelect.Items.Add("=");
        ddlSelect.Items.Insert(0, "All");
    }
    protected void GetProductStatus()
    {
       // DataSet stock = stockMgmt.GetProdustStockHistory();

        //3 tier architecture
        DataSet stock = stockManagementDAS.GetProdustStockHistory(); 
        DataView view = new DataView(stock.Tables[0]);
        string condition = null;
        if (grvStockStatus.HeaderRow != null)
        {
            DropDownList ddlSelect = (DropDownList)grvStockStatus.HeaderRow.FindControl("ddlSelect");
            DropDownList ddlSelect1 = (DropDownList)grvStockStatus.HeaderRow.FindControl("ddlSelect1");
            DropDownList ddlSelect2 = (DropDownList)grvStockStatus.HeaderRow.FindControl("ddlSelect2");

            string ddlSel = ddlSelect.SelectedValue;
            string ddSel1=ddlSelect1.SelectedValue;
            string ddSel2 = ddlSelect2.SelectedValue;
            if (ddlSel != null && ddlSel!="All")
            {
                TextBox txtProductCode = (TextBox)grvStockStatus.HeaderRow.FindControl("txtProductCodee");
                if (txtProductCode.Text.Trim() == null || txtProductCode.Text.Trim() == "")
                {
                    MessageBox.Show("Pease enter ProductCode");
                }
                else
                {
                    if (condition == null)
                    {
                        if (ddlSel == "Like")
                            condition = "ProductCode " + "like '" + '%' + "' + '" + txtProductCode.Text + "' + '" + '%' + "'";
                        else
                            condition = "ProductCode " + ddlSel + " '" + txtProductCode.Text.Trim() + "'";
                    }
                    else
                    {
                        if (ddlSel == "Like")
                            condition += "and ProductCode " + "like '" + '%' + "' + '" + txtProductCode.Text + "' + '" + '%' + "'";
                        else
                            condition += "and ProductCode " + ddlSel + " '" + txtProductCode.Text.Trim() + "'";
                    }

                }

            }
            if (ddSel1 != null && ddSel1 != "All")
            {
                TextBox txtProductName = (TextBox)grvStockStatus.HeaderRow.FindControl("txtProdName");
                if (txtProductName.Text.Trim() == null || txtProductName.Text.Trim() == "")
                {
                    MessageBox.Show("Pease enter ProductName");
                }
                else
                {
                    if (condition == null)
                    {
                        if (ddSel1 == "Like")
                            condition = "ProductName " + "like '" + '%' + "' + '" + txtProductName.Text + "' + '" + '%' + "'";
                        else
                            condition = "ProductName " + ddSel1 + " '" + txtProductName.Text.Trim() + "'";
                    }
                    else
                    {
                        if (ddSel1 == "Like")
                            condition += "and ProductName " + "like '" + '%' + "' + '" + txtProductName.Text + "' + '" + '%' + "'";
                        else
                            condition += "and ProductName " + ddSel1 + " '" + txtProductName.Text.Trim() + "'";

                    }
                }
            }

            if (ddSel2 != null && ddSel2 != "All")
            {
                TextBox txtQuantity = (TextBox)grvStockStatus.HeaderRow.FindControl("txtQty");
                if (txtQuantity.Text.Trim() == null || txtQuantity.Text.Trim() == "")
                {
                    MessageBox.Show("Pease enter Quantity");
                }
                else
                {
                    if (condition == null)
                    {
                        condition = "Quantity " + ddSel2 + " '" + txtQuantity.Text.Trim() + "'";
                    }
                    else
                    {
                        condition += " and Quantity " + ddSel2 + " '" + txtQuantity.Text.Trim() + "'";
                    }
                }
            }
        }
        if (condition != null)
            view.RowFilter = condition;
        grvStockStatus.DataSource = view;
        grvStockStatus.DataBind();
    }
    protected void grvStockStatus_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.Header)
        {
            DropDownList ddl = (DropDownList)e.Row.FindControl("ddlSelect");
            DropDownList ddl1 = (DropDownList)e.Row.FindControl("ddlSelect1");
            DropDownList ddl2 = (DropDownList)e.Row.FindControl("ddlSelect2");
            PopulateProductCode(ddl);
            PopulateProductCode(ddl1);
            PopulateCondition(ddl2);
        }
    }
    protected void grvStockStatus_SelectedIndexChanged(object sender, EventArgs e)
    {

    }


    //private void BindGridView()
    //{
    //    DataView dv = GetAllCustomers();
    //    string condition = null;

    //    if (GridView1.HeaderRow != null)
    //    {
    //        DropDownList cmbCustomerType = (DropDownList)GridView1.HeaderRow.FindControl("cmbCustomerType");
    //        CustomerType = cmbCustomerType.SelectedValue;

    //        DropDownList cmbGender = (DropDownList)GridView1.HeaderRow.FindControl("cmbGender");
    //        Gender = cmbGender.SelectedValue;
    //        if (cmbCustomerType.SelectedValue != "All")
    //        {
    //            condition = "Cus_Type='" + cmbCustomerType.SelectedValue + "'";
    //        }

    //        if (cmbGender.SelectedValue != "All")
    //        {
    //            if (condition != null)
    //                condition += " and Cus_ Gender ='" + cmbGender.SelectedValue + "'";
    //            else
    //                condition = "Cus_ Gender ='" + cmbGender.SelectedValue + "'";
    //        }
    //    }


    //    if (condition != null)
    //        dv.RowFilter = condition;

    //    GridView1.DataSource = dv;
    //    GridView1.DataBind();
    //}
    protected void BtnSearch_Click(object sender, EventArgs e)
    {

    }
    protected void grvStockStatus_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        grvStockStatus.PageIndex = e.NewPageIndex;
        GetProductStatus();
    }
}
